#include <QFileDialog>
#include "mainapp.h"
#include "ui_mainapp.h"
#include "filterwindow.h"
#include "splotdialog.h"
#include "nonlinearfilterdialog.h"
#include "segmentationviewdialog.h"
#include "infodialog.h"
#include <QDebug>
#include <QMouseEvent>
#include <windows.h>


MainApp::MainApp(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainApp)
{
    ui->setupUi(this);

    m_Img = new CImageProc();

    activeOnMouse = false;

    QPixmap pixmap;



    if (!pixmap.load( "D:/Programming/QT/Projects/POID/rsc/def.png" ))
        qWarning("Failed to load");

    m_OrigWnd = NULL;
    m_ProcWnd = NULL;

    m_FileOpened = false;
}

MainApp::~MainApp()
{
    delete ui;
}

void MainApp::on_actionWyj_cie_triggered()
{
    delete m_OrigWnd;
    delete m_ProcWnd;
    exit(EXIT_SUCCESS);
}

void MainApp::on_actionOtw_rz_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "../poid-google/img/poid", tr("Files (*.*)"));
    QPixmap pixmap;

    if (!pixmap.load(fileName))
        qWarning("Failed to load");

   // pixmap = pixmap.scaled(256, 256);

   // ui->ImgOrigLabel->setPixmap( pixmap );
   //  m_ProcWnd->m_Label->setPixmap( pixmap );

    if (!m_OrigWnd)
    {
        m_OrigWnd = new ImgWindow(this);
        m_OrigWnd->setGeometry((600 - pixmap.width()) / 2, 100, pixmap.width(), pixmap.height());
        m_OrigWnd->setWindowTitle("Obraz oryginalny");
        m_OrigWnd->m_Label = new QLabel(m_OrigWnd);
        m_OrigWnd->m_Label->setFrameShape(QFrame::Shape::Box);
        xOrig = (600 - pixmap.width()) / 2;
        yOrig = 100;
        qDebug() << "x: " << ((600 - pixmap.width()) / 2);
        qDebug() << "y: " << 100;
        widthOrig = pixmap.width();
        heightOrig = pixmap.height();
    }

    m_OrigWnd->m_Label->resize(pixmap.width(), pixmap.height());
    m_OrigWnd->m_Label->setPixmap( pixmap );
    m_OrigWnd->show();

    if (!m_ProcWnd)
    {
        m_ProcWnd = new ImgWindow(this);
        m_ProcWnd->setGeometry(600 + (600 - pixmap.width()) / 2, 100, pixmap.width(), pixmap.height());
        m_ProcWnd->setWindowTitle("Obraz przetwarzany");
        m_ProcWnd->m_Label = new QLabel(m_ProcWnd);
        m_ProcWnd->m_Label->setFrameShape(QFrame::Shape::Box);
        xProc = 600 + (600 - pixmap.width()) / 2;
        yProc = 100;
    }

    m_ProcWnd->m_Label->resize(pixmap.width(), pixmap.height());
    m_ProcWnd->m_Label->setPixmap( pixmap );
    m_ProcWnd->show();

    ui->label_7->setText(QString::number(ui->horizontalSlider->value()));
    ui->label_8->setText(QString::number(ui->horizontalSlider_2->value()));

    m_Img->m_Fourier->SetData(new QImage(pixmap.toImage()));
    m_Img->m_Segmentation->SetData(new QImage(pixmap.toImage()));
    m_FileOpened = true;
    buttonFlag = false;
}

void MainApp::on_actionZapisz_triggered()
{
    if (m_FileOpened)
    {
        QFile file(QFileDialog::getSaveFileName(this, tr("Save File"), "../poid-google/img/poid"));
        file.open(QIODevice::WriteOnly);
         m_ProcWnd->m_Label->pixmap()->save(&file, "BMP");
    }
}

void MainApp::on_pushButton_clicked()
{
    if (m_FileOpened)
    {
        m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
         m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->Negative()));
    }
}

void MainApp::on_horizontalSlider_valueChanged(int value)
{
    if (m_FileOpened)
    {
        QImage *img = new QImage( m_OrigWnd->m_Label->pixmap()->toImage());
        m_Img->SetData(img);
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->Brightness(value)));
        ui->label_7->setText(QString::number(m_Img->m_Brightness));
    }
}

void MainApp::on_actionCofnij_triggered()
{
    if (m_FileOpened)
    {
        QImage *img = m_Img->GetPreviousData();

        if (img)
             m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*img));
    }
}

void MainApp::on_pushButton_2_clicked()
{
    if (m_FileOpened)
    {
        ui->horizontalSlider->setValue(0);
        ui->horizontalSlider_2->setValue(101);
         m_ProcWnd->m_Label->setPixmap((*m_OrigWnd->m_Label->pixmap()));
    }
}

void MainApp::on_horizontalSlider_2_valueChanged(int value)
{
    if (m_FileOpened)
    {
        QImage *img = new QImage( m_OrigWnd->m_Label->pixmap()->toImage());
        m_Img->SetData(img);
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->Contrast(value)));
        ui->label_8->setText(QString::number(m_Img->m_Contrast));
    }
}

void MainApp::on_pushButton_3_clicked()
{
    if (m_FileOpened)
    {
        ui->horizontalSlider->setValue(0);
        ui->horizontalSlider_2->setValue(101);
         m_ProcWnd->m_Label->setPixmap((*m_OrigWnd->m_Label->pixmap()));
    }
}

void MainApp::on_action_rednia_arytmetyczna_triggered()
{
    if (m_FileOpened)
    {
        FilterWindow wndDialog = FilterWindow(0, this);
        wndDialog.setModal(true);
        m_ArithmeticFilterTriggered = true;
        wndDialog.exec();
    }
}

void MainApp::SetWindowForFilters(int _type)
{
    switch(_type)
    {
        case 0:
            m_Img->m_FilterWindowSize = 3;
        break;

        case 1:
            m_Img->m_FilterWindowSize = 5;
        break;

        case 2:
            m_Img->m_FilterWindowSize = 7;
        break;

        default:
            m_Img->m_FilterWindowSize = 3;
    }

    if (m_ArithmeticFilterTriggered)
    {
        m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->ArithmeticFilter(m_OrigWnd->m_Label->pixmap()->toImage())));
    }
    else
    {
        m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
         m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->MedianFilter(m_OrigWnd->m_Label->pixmap()->toImage())));
    }
}

void MainApp::on_actionMediana_triggered()
{
    if (m_FileOpened)
    {
        FilterWindow wndDialog = FilterWindow(0, this);
        wndDialog.setModal(true);
        m_ArithmeticFilterTriggered = false;
        wndDialog.exec();
    }
}

void MainApp::on_actionHistogram_jasno_ci_triggered()
{
    if (m_FileOpened)
    {
        m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
        mHistDialog = new HistogramDialog(0, this);
        mHistDialog->setModal(true);
        mHistDialog->SetHistogramR(m_Img->Histogram( m_ProcWnd->m_Label->pixmap()->toImage(), 0));
        mHistDialog->SetHistogramG(m_Img->Histogram( m_ProcWnd->m_Label->pixmap()->toImage(), 1));
        mHistDialog->SetHistogramB(m_Img->Histogram( m_ProcWnd->m_Label->pixmap()->toImage(), 2));
        mHistDialog->exec();
    }
}

void MainApp::ModifyImgByHistogram(int _gMin, float _alpha)
{
    qDebug() << "dupa histogram";
    m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
    QImage histogram = QImage(256, 256, m_Img->GetData()->format());
     m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage((*m_Img->ModifyImgByHistogramGrayScale( m_ProcWnd->m_Label->pixmap()->toImage(), histogram, _gMin, _alpha))));
    mHistDialog->SetHistogramR(&histogram);

    /*
    mHistDialog->SetHistogramR(m_Img->ModifyImgByHistogram( m_ProcWnd->m_Label->pixmap()->toImage(), 0, _gMin));
    mHistDialog->SetHistogramG(m_Img->ModifyImgByHistogram( m_ProcWnd->m_Label->pixmap()->toImage(), 1, _gMin));
    mHistDialog->SetHistogramB(m_Img->ModifyImgByHistogram( m_ProcWnd->m_Label->pixmap()->toImage(), 2, _gMin));
    */
}

void MainApp::on_actionSplot_triggered()
{
    if (m_FileOpened)
    {
        SplotDialog spltDialog = SplotDialog(0, this);
        spltDialog.setModal(true);
        spltDialog.exec();
    }
}

void MainApp::SetSplotMask(int _mask)
{
    m_Img->m_SplotMask = _mask;
    m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
    m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->Splot(m_OrigWnd->m_Label->pixmap()->toImage())));
}

void MainApp::DoNonLinearFilter(int _R)
{
    m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
    m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->NonLinearFilter(m_OrigWnd->m_Label->pixmap()->toImage(), _R)));
}

void MainApp::on_actionFiltracja_nieliniowa_triggered()
{
    if (m_FileOpened)
    {
        NonLinearFilterDialog nonLnrDialog = NonLinearFilterDialog(0, this);
        nonLnrDialog.setModal(true);
        nonLnrDialog.exec();
    }
}

void MainApp::on_actionO_programie_triggered()
{
    InfoDialog infDialog = InfoDialog(0);
    infDialog.setModal(true);
    infDialog.exec();
}
void MainApp::on_actionIFFT_triggered(){
    if (m_FileOpened)
    {
        m_Img->m_Fourier->IFFT();
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData()));
        //m_OrigWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetDataData()));
    }
}
void MainApp::on_actionFFT_triggered()
{

}
void MainApp::on_actionSegmentacja_triggered(){
    if (m_FileOpened)
    {
        mSegDialog = new SegmentationDialog(0, this);
        mSegDialog->setModal(true);

        mSegDialog->exec();
        activeOnMouse = true;
    }
}
void MainApp::mousePressEvent( QMouseEvent * event ){
    if((event->x() > xOrig) && (event->x() < (xOrig + widthOrig)) && (event->y() > yOrig) && (event->y() < (yOrig + heightOrig)) && activeOnMouse==true){
      if((mSegDialog->ready4Seg)==true){
          qDebug() << "TAK!";
            m_Img->m_Segmentation->Clicked(event->x()- xOrig, event->y()-yOrig);
            m_Img->m_Segmentation->AllSegments(mSegDialog->allSegments);
            m_Img->m_Segmentation->SetTolerance(mSegDialog->tolerance);
            m_Img->m_Segmentation->FindSegments();

            m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Segmentation->GetSegments()));
            ui->pushButtonSeg->setEnabled(true);
        }
      else
          qDebug() << "NIE!";
    }
    else{
        qDebug() << "Kliknij w obrazek";
    }

}
void MainApp::on_pushButtonSeg_clicked(){
    /*qDebug()<<"ma wsykoczyć okienko";
    //qDebug()<<;
    mSegViewDialog = new SegmentationViewDialog(0, this);
    qDebug()<<"dupa";
    //setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData())
    mSegViewDialog->SetWindow((int)widthOrig,(int)heightOrig, m_Img->GetData());
    qDebug()<<"dupa1";
    mSegViewDialog->setModal(true);
    qDebug()<<"dupa2w";
    mSegViewDialog->exec();
    activeOnMouse = true;


    ui->pushButtonSeg->setEnabled(false);
    */
    buttonFlag = !buttonFlag;
    qDebug()<<buttonFlag;
    if(buttonFlag)
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Segmentation->GetData()));
    else
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Segmentation->GetSegments()));

}

void MainApp::on_actionWidmo_mocy_triggered()
{
    if (m_FileOpened)
    {
        m_Img->m_Fourier->FFT();
        if(m_Img->m_Fourier->IsGrey()){
            //qDebug()<<"szary";
            m_Img->m_Fourier->FFT2Img(m_Img->m_Fourier->fft, true);
        }
        else{
           m_Img->m_Fourier->FFT2ImgRGB(m_Img->m_Fourier->fftR,m_Img->m_Fourier->fftG,m_Img->m_Fourier->fftB, true);
        }
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData()));
    }
}

void MainApp::on_actionWidmo_fazy_triggered()
{
    if (m_FileOpened)
    {
        m_Img->m_Fourier->FFT();
        if(m_Img->m_Fourier->IsGrey()){
            //qDebug()<<"szary";
            m_Img->m_Fourier->FFT2Img(m_Img->m_Fourier->fft, false);
        }
        else{
           m_Img->m_Fourier->FFT2ImgRGB(m_Img->m_Fourier->fftR,m_Img->m_Fourier->fftG,m_Img->m_Fourier->fftB, false);
        }

        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData()));
    }
}

void MainApp::on_actionFiltr_G_rnoprzepustowy_triggered()
{
    if (m_FileOpened)
    {
        m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
        m_HPFDialog = new HighPassFilterDialog(0, this);
        m_HPFDialog->setModal(true);
        m_HPFDialog->exec();
    }
}

void MainApp::on_actionFiltr_modyfikuj_cy_faze_triggered()
{
    if (m_FileOpened)
    {
        m_Img->SetData(new QImage( m_ProcWnd->m_Label->pixmap()->toImage()));
        m_PModFDialog = new PhaseFilterDialog(0, this);
        m_PModFDialog->setModal(true);
        m_PModFDialog->exec();
    }
}

void MainApp::HighPassFilter(float _r)
{
    m_Img->m_Fourier->FFT();

    if(m_Img->m_Fourier->IsGrey())
    {
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fft, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->HighPassFilter(_r);
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->FFT2Img(m_Img->m_Fourier->fft, true)));
        m_Img->m_Fourier->IFFT();
        m_OrigWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData()));
    }
    else
    {
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fftR, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->HighPassFilter(_r);
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->FFT2Img(m_Img->m_Fourier->fftR, true)));        // Just for show
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fftG, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->HighPassFilter(_r);
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fftB, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->HighPassFilter(_r);
        m_Img->m_Fourier->IFFT();
        m_OrigWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData()));
    }
}

void MainApp::PhaseModFilter(int _k, int _l)
{
    m_Img->m_Fourier->FFT();

    if(m_Img->m_Fourier->IsGrey())
    {
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fft, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->m_FFTData = m_Img->m_Fourier->fft;
        m_Img->m_Filter->PhaseModFilter(_k, _l);
        //m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->FFT2Img(m_Img->m_Filter->m_FFTData, true)));
        m_Img->m_Fourier->IFFT();
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData()));
    }
    else
    {
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fftR, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->m_FFTData = m_Img->m_Fourier->fftR;
        m_Img->m_Filter->PhaseModFilter(_k, _l);
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fftG, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->m_FFTData = m_Img->m_Fourier->fftG;
        m_Img->m_Filter->PhaseModFilter(_k, _l);
        m_Img->CreateHighPassFilter(m_Img->m_Fourier->fftB, m_Img->GetImgWidth(), m_Img->GetImgHeight());
        m_Img->m_Filter->m_FFTData = m_Img->m_Fourier->fftB;
        m_Img->m_Filter->PhaseModFilter(_k, _l);
        m_Img->m_Fourier->IFFT();
        m_ProcWnd->m_Label->setPixmap(QPixmap::fromImage(*m_Img->m_Fourier->GetData()));
    }
}
